/*
 * @Author: silence
 * @Date: 2023-08-18 10:03:29
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-18 08:54:01
 * @Description: 配网第三步：链接设备Wi-Fi
 */

import 'package:ems_neutral_app/pages/DeviceNetworking/DNSetProgressPage.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../base/SilScaffold.dart';
import '../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../../utils/color/ColorUtils.dart';
import 'Controller/DeviceNetworkingController.dart';

class DNLinkDeviceWifiPage extends StatefulWidget {
  const DNLinkDeviceWifiPage({super.key});

  @override
  State<DNLinkDeviceWifiPage> createState() => _DNLinkDeviceWifiPageState();
}

class _DNLinkDeviceWifiPageState extends State<DNLinkDeviceWifiPage>
    with WidgetsBindingObserver {
  bool _isOpenWifiSettings = false; // 是否打开了wifi设置
  DeviceNetworkingController _controller =
      Get.put(DeviceNetworkingController());

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this); // 注册监听器
  }

  /// 处理从设置返回
  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed &&
        this.mounted &&
        _isOpenWifiSettings) {
      _isOpenWifiSettings = false;
      ARoute.gotoPage(context, DNSetProgressPage());
    }
    super.didChangeAppLifecycleState(state);
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      title:
          LangOf(context)?.deviceNetworking?.connectDeviceWifi, // '连接设备Wi-Fi',
      body: mainWidget(),
    );
  }

  mainWidget() {
    return Column(
      children: [
        Expanded(child: contentView()),
        bottomBtn(),
      ],
    );
  }

  /// 内容
  contentView() {
    return ListView(children: [
      Padding(padding: EdgeInsets.only(top: 20.w)),

      /// 提示文字
      SilText(
        LangOf(context)
            ?.deviceNetworking
            ?.connectDeviceWifiPrompt, //'1.请前往WLAN设置，手动连接设备网络。\n2.部分设备在连接网络时，可能需要输入密码，此密码可在设备外壳上找到。\n3.成功连接网络后，返回APP。',
        color: ColorUtils.color_font_main,
        fontSize: 14.sp,
        maxLines: 0,
        lineHeight: 1.5.w,
      ),

      /// 图片
      SilImage(
        'imgs/device_net/device_net_device_wifi_prompt.png',
        margin: EdgeInsets.only(top: 90.w),
      )
    ]);
  }

  /// 底部按钮
  bottomBtn() {
    return SubmitBtn(
      margin: EdgeInsets.only(
          top: 10.w, bottom: MediaQuery.of(context).padding.bottom + 10.w),
      title: LangOf(context)?.deviceNetworking?.goConnect, //'去连接'
      onTapFunc: () async {
        /// wifi开关打开，并且有定位权限
        bool isLocationEnabled =
            await _controller.checkWifiSwitchAndLocalPower(context);
        if (isLocationEnabled) {
          _controller.openWifiSettings();
          _isOpenWifiSettings = true;
        }
      },
    );
  }
}
